<?php
namespace Admin\Models\ask;

use Admin\Models\BaseModel;

class XhaskQuestion extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iAQId;

    /**
     *
     * @var string
     */
    public $sQueTitle;

    /**
     *
     * @var string
     */
    public $sQueExplain;

    /**
     *
     * @var integer
     */
    public $iQueType;

    /**
     *
     * @var integer
     */
    public $iChooseNum;

    /**
     *
     * @var integer
     */
    public $bRequire;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHAsk_Question");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHAsk_Question';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhaskQuestion[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhaskQuestion
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据传入id，删除对应记录并返回删除结果
     *
     * @param $ids
     * @return mixed
     *
     * create by ck 20160924
     */
    public static function deleteRecordById($ids){
        return self::find(array(
            'conditions' => "iAQId IN ({$ids}) AND bDelete = 0"
        ))->delete();
    }

    /**
     * 根据id查找是否存在记录，存在则修改数据，否则新建
     *
     * @param array $data
     * @return XhaskQuestion
     *
     * create by ck 20160722
     * modify by ck 20160924
     */
    public static function createRecord($data)
    {
        $obj = $data['iAQId'] ? self::findFirst($data['iAQId']) : new self;
        $obj->assign($data);
        return $obj;
    }

    /**
     * 根据题库ID字符串数组获取题目信息
     *
     * @param $ids
     * @return mixed
     *
     * create by ck 20160926
     */
    public static function getQuestionById($ids)
    {
        return self::find(array(
            'columns'       => 'iAQId,sQueTitle,sQueExplain,iQueType,iChooseNum,bRequire',
            'conditions'    => "bDelete=0 AND iAQId IN ({$ids})"
        ))->toArray();
    }

}
